//
//  DealDetail.m
//  BrushFace
//
//  Created by timer_q on 16/1/27.
//  Copyright © 2016年 Zzz. All rights reserved.
//


#import "DealInfo.h"
#import "DealBigCell.h"
#import "DealdefaultCell.h"

@interface DealInfo ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSDictionary *_currDict;
#pragma mark------xuxuejiao
    
    NSString *_stateString;
    
#pragma mark----------------
}
@end

@implementation DealInfo

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UIConfig];
    [self.navBar setTitle:@"交易详情"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];

    
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSString *requestStr = [NSString stringWithFormat:@"%@/user/gettradelogInfo4app.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"tradeLogId":self.infoid};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        _currDict = jsondict;
        [_tableView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}
- (void)UIConfig{
    self.view.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 75, SCREEN_WIDTH-20, SCREEN_HEIGHT-75) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.type integerValue] == 2||[self.type integerValue] == 4) {
        return 5;
    }else if([self.type integerValue] == 0||[self.type integerValue]==1||[self.type integerValue]==3||[self.type integerValue]==5){
        return 6;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
//    if (self.type == 0&&indexPath.row == 4) {
//        DealBigCell *cell = [DealdefaultCell loadBundleNib];
//        
//        return cell;
//    }
    
    if ([self.type integerValue]==2||[self.type integerValue]==4) {
        if (indexPath.row==4) {
            DealdefaultCell *cell = [DealdefaultCell loadBundleNib];
            cell.cellTitle.text = @"交易状态";
            cell.cellDetail.text = [NSString stringWithFormat:@"交易%@",self.stateStr];
            return cell;
        }
    }
    if(indexPath.row == 0){
    DealdefaultCell *cell = [DealdefaultCell loadBundleNib];
        switch ([self.type integerValue]) {
            case 0:
            {
                cell.cellName.text = @"还款详情";
            }
                break;
            case 1:
            {
                cell.cellName.text = @"充值详情";
            }
                break;
            case 2:
            {
                cell.cellName.text = @"借入详情";
            }
                break;
            case 3:
            {
                cell.cellName.text = @"借出详情";
            }
                break;
            case 4:
            {
                cell.cellName.text = @"回款详情";
            }
                break;
            case 5:
            {
                cell.cellName.text = @"提现详情";
            }
                break;
            default:
                break;
        }
        cell.cellName.hidden = NO;
        cell.cellDetail.hidden = YES;
        cell.cellTitle.hidden = YES;
    return cell;
    }else{
    DealdefaultCell *cell = [DealdefaultCell loadBundleNib];
        cell.cellName.hidden = YES;
        cell.cellDetail.hidden = NO;
        cell.cellTitle.hidden = NO;
        switch (indexPath.row) {
            case 1:
            {
                cell.cellTitle.text = @"交易类型";
                switch ([self.type integerValue]) {
                    case 0:
                    {
                        cell.cellDetail.text = @"还款";
                    }
                        break;
                    case 1:
                    {
                        cell.cellDetail.text = @"充值";
                    }
                        break;
                    case 2:
                    {
                        cell.cellDetail.text = @"借入";
                    }
                        break;
                    case 3:
                    {
                        cell.cellDetail.text = @"借出";
                    }
                        break;
                    case 4:
                    {
                        cell.cellDetail.text = @"回款";
                    }
                        break;
                    case 5:
                    {
                        cell.cellDetail.text = @"提现";
                    }
                        break;
                    default:
                        break;
                }

            }
                break;
            case 2:
            {
                cell.cellTitle.text = @"交易时间";
                cell.cellDetail.text = [self timerStrWithTime:_currDict[@"tradetime"]];
            }
                break;
            case 3:
            {
                switch ([self.type integerValue]) {
                    case 0:
                    {
                        cell.cellTitle.text = @"还款金额";
                    }
                        break;
                    case 1:
                    {
                        cell.cellTitle.text = @"充值金额";
                    }
                        break;
                    case 2:
                    {
                        cell.cellTitle.text = @"借入金额";
                    }
                        break;
                    case 3:
                    {
                        cell.cellTitle.text = @"借出金额";
                    }
                        break;
                    case 4:
                    {
                        cell.cellTitle.text = @"回款金额";
                    }
                        break;
                    case 5:
                    {
                        cell.cellTitle.text = @"提现金额";
                    }
                        break;
                    default:
                        break;
                }
                cell.cellDetail.text = [NSString stringWithFormat:@"%.2f",[_currDict[@"amount"] floatValue]];

            }
                break;
            case 4:
            {
                switch ([self.type integerValue]) {
                    case 0:
                    {
                        cell.cellTitle.text = @"还款方式";
                    }
                        break;
                    case 1:
                    {
                        cell.cellTitle.text = @"充值方式";
                    }
                        break;
                    case 2:
                    {
                        //cell.cellTitle.text = @"借";
                    }
                        break;
                    case 3:
                    {
                        cell.cellTitle.text = @"支付方式";
                    }
                        break;
                    case 4:
                    {

                    }
                        break;
                    case 5:
                    {
                        cell.cellTitle.text = @"提现方式";
                    }
                        break;
                    default:
                        break;
                }
                if ([[_currDict objectForKey:@"channel"] integerValue]==1) {
                    cell.cellDetail.text  = @"微信支付";
                }else{
                    cell.cellDetail.text  = _currDict[@"bankCodeNm"];
                }
            }
                break;
            case 5:
            {
                cell.cellTitle.text = @"交易状态";
                cell.cellDetail.text = [NSString stringWithFormat:@"交易%@",self.stateStr];
            }
                break;
            default:
                break;
        }
        
        
    return cell;
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if ([self.type integerValue]==0&&indexPath.row==4) {
//        return 81;
//    }
    return 51;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSString *)timerStrWithTime:(NSString *)time{
    double timeValue = [time doubleValue]/1000;
    NSDate *detaildate= [NSDate dateWithTimeIntervalSince1970:timeValue];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

@end
